package cn.bdqn.layui.service.rabbit;


import cn.bdqn.layui.entity.vo.MailDto;
import cn.bdqn.layui.entity.vo.ProUserVo;
import cn.bdqn.layui.entity.ProductSuccess;
import cn.bdqn.layui.mapper.ProductSuccessMapper;
import cn.bdqn.layui.service.mail.MailService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 接收消息服务
 */
@Service
public class RabbitReceiverService {


    @Resource
    MailService mailService;

    @Resource
    private ProductSuccessMapper productSuccessMapper;

    /**
     * 接收消息秒杀成功 异步邮件通知
     */
    @RabbitListener(queues = "email.queue")
    public void consumeEmailMsg(ProUserVo proUserVo) {
        String format = String.format("您好，您已成功抢购到商品: <strong style=\"color: red\">%s</strong> ，复制该链接并在浏览器采用新的页面打开，即可查看抢购详情：${system.domain.url}/kill/record/detail/%s，并请您在1个小时内完成订单的支付，超时将失效该订单哦！祝你生活愉快！", proUserVo.getProductName(), proUserVo.getCode());
        if (proUserVo.getEmail() == null) {
            proUserVo.setEmail("1150392623@qq.com");
        }
        MailDto dto = new MailDto("商品抢购成功", format, new String[]{proUserVo.getEmail()});

        //发送html邮件信息
        mailService.sendHTMLMail(dto);
    }

    /**
     * 秒杀成功 超时未支付
     */
    @RabbitListener(queues = "kill.dead.real.queue")
    public void consumeEmailOrder(ProUserVo proUserVo) {
        /*String format = String.format("您好，您已成功抢购到商品: <strong style=\"color: red\">%s</strong> ，复制该链接并在浏览器采用新的页面打开，即可查看抢购详情：${system.domain.url}/kill/record/detail/%s，并请您在1个小时内完成订单的支付，超时将失效该订单哦！祝你生活愉快！", info.getItemName(), info.getCode());
        MailDto dto = new MailDto("商品抢购成功", format, new String[]{info.getEmail()});
        mailService.sendHTMLMail(dto);*/
        if (proUserVo != null) {
            ProductSuccess productSuccess = productSuccessMapper.selectByPrimaryKey(proUserVo.getCode());
            if (productSuccess != null && productSuccess.getStatus().intValue() == 0) {
                productSuccessMapper.expireOrder(proUserVo.getCode());

            }
        }
    }
}
